package com.tjcm.tjcminspectmanager;

import java.util.ArrayList;
import java.util.List;

import com.tjcm.tjcminspectmanager.manager.SPManager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.adapterview.DataHolder;
import cn.emagsoftware.ui.adapterview.GenericAdapter;
import cn.emagsoftware.ui.adapterview.ViewHolder;
import cn.emagsoftware.ui.staticmanager.StaticManager;

public class UploadListActivity extends BaseActivity
{

    private Handler  handler       = new Handler();
    private boolean  isInterrupted = false;
    private ListView list          = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (!LoginActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, LoginActivity.class);
            return;
        }
        setContentView(R.layout.uploadlist);

        list = (ListView) findViewById(R.id.list);
        final Button stop = (Button) findViewById(R.id.stop);
        Button back = (Button) findViewById(R.id.back);
        loopRefresh(true);
        if (MainActivity.backgroundTask.isRunning())
            stop.setText(R.string.main_menu_background_btn_stop);
        else
            stop.setText(R.string.main_menu_background_btn_start);
        stop.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                if (MainActivity.backgroundTask.isRunning())
                {
                    MainActivity.backgroundTask.stop();
                    stop.setText(R.string.main_menu_background_btn_start);
                } else
                {
                    MainActivity.backgroundTask.start();
                    stop.setText(R.string.main_menu_background_btn_stop);
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }

    private void loopRefresh(boolean isFirst)
    {
        String[] queue = SPManager.getUploadTaskQueue(this);
        List<DataHolder> holders = new ArrayList<DataHolder>();
        for (String item : queue)
        {
            holders.add(new DataHolder(item, 0)
            {
                @Override
                public View onCreateView(Context arg0, int arg1, Object arg2)
                {
                    // TODO Auto-generated method stub
                    View item = LayoutInflater.from(arg0).inflate(R.layout.uploadlist_item, null);
                    item.setTag(new ViewHolder(item.findViewById(R.id.no), item.findViewById(R.id.text)));
                    onUpdateView(arg0, arg1, item, arg2);
                    return item;
                }

                @Override
                public void onUpdateView(Context arg0, int arg1, View arg2, Object arg3)
                {
                    // TODO Auto-generated method stub
                    View[] views = ((ViewHolder) arg2.getTag()).getParams();
                    ((TextView) views[0]).setText((arg1 + 1) + "");
                    ((TextView) views[1]).setText((String) arg3);
                }

                @Override
                public void onAsyncDataExecuted(Context arg0, int arg1, View arg2, Object arg3, int arg4)
                {
                    // TODO Auto-generated method stub
                }
            });
        }
        list.setAdapter(new GenericAdapter(this, holders));
        if (isFirst && queue.length == 0)
            ToastManager.showLong(this, R.string.main_menu_background_empty);
        if (!isInterrupted)
        {
            handler.postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    // TODO Auto-generated method stub
                    loopRefresh(false);
                }
            }, 3000);
        }
    }

    @Override
    protected void onDestroy()
    {
        // TODO Auto-generated method stub
        super.onDestroy();
        isInterrupted = true;
    }

}